<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */
declare (strict_types = 1);
namespace app\api\controller;
use app\api\BaseController;
use app\note\model\Diagnosis as DiagnosisList;
use app\note\model\Note as NoteList;
use app\api\middleware\Auth;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
use think\facade\Request;

class Agricultura extends BaseController
{
    /**
     * 控制器中间件 [登录、注册 不需要鉴权]
     * @var array
     */
	protected $middleware = [
    	Auth::class => ['except' 	=> ['index','login'] ]
    ];
    /**
     * @param $user_id
     * @return string
     */
    public function getToken($user_id){
        $time = time(); //当前时间
		$conf = $this->jwt_conf;
        $token = [
            'iss' => $conf['iss'], //签发者 可选
            'aud' => $conf['aud'], //接收该JWT的一方，可选
            'iat' => $time, //签发时间
            'nbf' => $time-1 , //(Not Before)：某个时间点后才能访问，比如设置time+30，表示当前时间30秒后才能使用
            'exp' => $time+$conf['exptime'], //过期时间,这里设置2个小时
            'data' => [
                //自定义信息，不要定义敏感信息
                'userid' =>$user_id,
            ]
        ];
        return JWT::encode($token, $conf['secrect'], 'HS256'); //输出Token  默认'HS256'
    }

    /**
     * @param $token
     */
    public static function checkToken($token){
        try {
            JWT::$leeway = 60;//当前时间减去60，把时间留点余地
            $decoded = JWT::decode($token, self::$config['secrect'], ['HS256']); //HS256方式，这里要和签发的时候对应
            return (array)$decoded;
        } catch(\Firebase\JWT\SignatureInvalidException $e) {  //签名不正确
            return json(['code'=>403,'msg'=>'签名错误']);
        }catch(\Firebase\JWT\BeforeValidException $e) {  // 签名在某个时间点之后才能用
            return json(['code'=>401,'msg'=>'token失效']);
        }catch(\Firebase\JWT\ExpiredException $e) {  // token过期
            return json(['code'=>401,'msg'=>'token已过期']);
        }catch(Exception $e) {  //其他错误
            return json(['code'=>404,'msg'=>'非法请求']);
        }catch(\UnexpectedValueException $e) {  //其他错误
            return json(['code'=>404,'msg'=>'非法请求']);
        } catch(\DomainException $e) {  //其他错误
            return json(['code'=>404,'msg'=>'非法请求']);
        }

    }	
	
    /**
     * @api {post} /demo/login 会员登录
     * @apiDescription 系统登录接口，返回 token 用于操作需验证身份的接口

     * @apiParam (请求参数：) {string}             username 登录用户名
     * @apiParam (请求参数：) {string}             password 登录密码

     * @apiParam (响应字段：) {string}             token    Token

     * @apiSuccessExample {json} 成功示例
     * {"code":0,"msg":"登录成功","time":1627374739,"data":{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhcGkuZ291Z3VjbXMuY29tIiwiYXVkIjoiZ291Z3VjbXMiLCJpYXQiOjE2MjczNzQ3MzksImV4cCI6MTYyNzM3ODMzOSwidWlkIjoxfQ.gjYMtCIwKKY7AalFTlwB2ZVWULxiQpsGvrz5I5t2qTs"}}
     * @apiErrorExample {json} 失败示例
     * {"code":1,"msg":"帐号或密码错误","time":1627374820,"data":[]}
     */
	
    public function login()
    {
        $param = get_params();
        if (empty($param['username']) || empty($param['password'])) {
            $this->apiError('参数错误');
        }
        // 校验用户名密码
        $user = Db::name('Admin')->where(['username' => $param['username']])->find();
        if (empty($user)) {
            $this->apiError('帐号或密码错误');
        }
        $param['pwd'] = set_password($param['password'], $user['salt']);
        if ($param['pwd'] !== $user['pwd']) {
            $this->apiError('帐号或密码错误');
        }
        if ($user['status'] == -1) {
            $this->apiError('该用户禁止登录,请于平台联系');
        }
        $data = [
            'last_login_time' => time(),
            'last_login_ip' => request()->ip(),
            'login_num' => $user['login_num'] + 1,
        ];
        $res = Db::name('Admin')->where(['id' => $user['id']])->update($data);
        if ($res) {
            $token = self::getToken($user['id']);
            $this->apiSuccess('登录成功', ['token' => $token]);
        }
    }
    /**
     * @api {post} /index/demo 测试页面
     * @apiDescription  返回文章列表信息

     * @apiParam (请求参数：) {string}  token Token

     * @apiSuccessExample {json} 响应数据样例
     * {"code":1,"msg":"","time":1563517637,"data":{"id":13,"email":"test110@qq.com","password":"e10adc3949ba59abbe56e057f20f883e","sex":1,"last_login_time":1563517503,"last_login_ip":"127.0.0.1","qq":"123455","mobile":"","mobile_validated":0,"email_validated":0,"type_id":1,"status":1,"create_ip":"127.0.0.1","update_time":1563507130,"create_time":1563503991,"type_name":"注册会员"}}
     */
    public function test(Request $request)
    {
		/*$uid = JWT_UID;
        $userInfo = Db::name('Admin')->where(['id' => $uid])->find();
        $this->apiSuccess('请求成功', ['user' => $userInfo]);*/
        echo phpinfo();
    }
    /**
     * 用户信息修改
     */
    public function userinfo_update(){
        $param = get_params();
        if(empty($param['id'])){
            return to_assign(1,'编号不能为空');
        }
        /*if($param['id']!=$this->uid){
            return to_assign(1,'信息不相符');
        }*/
        if(isset($param['username'])){
            $datas['name']=$param['username'];
            $datas['nickname']=$param['username'];
        }
        if(isset($param['thumb_id'])){
            $thumb = Db::name('file')
                                ->where(['id'=>$param['thumb_id']])
                                ->find();
            if(!empty($thumb)){
                $datas['thumb'] =$thumb['filepath'];
            }
        }
        if(isset($param['thumb'])){
                $datas['thumb'] =$param['thumb'];
        }
        $update = Db::name('admin')->where(['id' => $param['id']])->update($datas);
        if($update){
            return to_assign(200, '修改成功', $datas);
        }else{
            return to_assign(1, '未做修改', $datas);
        }
    }
    /**
     * 专家中心-问诊列表
     */
    public function expert_center(){
        $param = get_params();
        $where = array();
        if (!empty($param['keywords'])) {
            $where[] = ['a.title', 'like', '%' . $param['keywords'] . '%'];
        }
        if(!empty($param['cate_id'])){
            $where[] = ['a.cate_id','=',$param['cate_id']];
        }
        if (!empty($param['industry_id'])) {
            $where[] = ['a.industry_id', 'in', $param['industry_id']];
        }
        if (!empty($param['status'])) {
            $where[] = ['a.status', '=', $param['status']];
        }else{
            $where[] = ['a.status', '=', 2];
        }
        if (!empty($param['expert_id'])) {
            $where[] = ['a.expert_id', '=', $param['expert_id']];
        }
        /*if($this->uid!=1){
            //$where[] = ['id', '=', $this->uid];
            $expert = Db::name('admin')->where(['id' => $this->uid])->find();
            $where[] = ['a.industry_id', 'in', $expert['industry_id']];
        }*/
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $note = DiagnosisList::where($where)
            ->field('a.*,c.title as cate_title,d.name,d.thumb')
            ->alias('a')
            ->leftjoin('industry c', 'a.industry_id = c.id')
            ->leftjoin('admin d', 'd.id = a.admin_id')
            //->leftjoin('file f', 'f.id = a.file_ids')
            //->order('a.end_time desc,a.sort desc,a.create_time desc')
            ->order('a.create_time desc')
            ->paginate($rows, false, ['query' => $param])
            ->each(function ($item, $key) {
                //$item->start_time = empty($item->start_time) ? '-' : date('Y-m-d', $item->start_time);
                //var_dump($item->create_time);exit;
                //$item->create_time = empty($item->create_time) ? '-' : date('Y-m-d H:i', int()$item->create_time);
                //pr($item->create_time);
                //$item->create_time = empty($item->create_time) ? '-' : date('Y-m-d',$item->create_time);
                //$item->end_time = empty($item->end_time) ? '-' : date('Y-m-d', int($item->end_time));
                $item->recover = Db::name('Recover')
                                ->alias('r')
                                ->field('r.*,d.name,d.thumb,FROM_UNIXTIME(r.create_time,"%Y-%m-%d %h:%i") as create_time')
                                ->where(['r.diagnosis_id'=>$item->id])
                                ->leftjoin('admin d', 'd.id = r.add_admin')
                                ->select();
                $file_ids=explode(',', $item->file_ids);        
                $item->imgs = Db::name('file')
                                ->field('filepath')
                                ->where('id','in',$item->file_ids)
                                ->select()->toArray();
            });
        $this->apiSuccess('请求成功',  $note,200);
    }
    /**
     * 专家回复
     */
    public function recover_add()
    {
        $param = get_params();
        if (empty($param['diagnosis_id'])) {
            $this->apiError('问诊编号不能为空','',1001);
        }
        if (empty($param['content'])) {
            $this->apiError('回复内容不能为空','',1001);
        }
        /*if (empty($param['expert_id'])) {
            $this->apiError('回复的专家编号不能为空','',1001);
        }*/
        if (empty($param['expert_id'])) {
            //$this->apiError('回复的专家编号不能为空','',1001);
            $param['expert_id']=$this->uid;
        }
        if (empty($param['status'])) {
            $update['status'] = 2;
        }else{
            $update['status'] = $param['status'];
        }
        $diagnosisRes = DiagnosisList::where(['id'=>$param['diagnosis_id']])->find();
        $param['industry_id'] = $diagnosisRes['industry_id'];
        $param['add_admin'] = $this->uid;
        $param['create_time'] = time();
        //$sid = DiagnosisList::strict(false)->field(true)->insertGetId($param);
        $sid = Db::name('Recover')->strict(false)->field(true)->insert($param);
        if ($sid) {
            $where['id'] = $param['diagnosis_id'];
            Db::name('diagnosis')->strict(false)->field(true)->where($where)->update($update);
            $param['recover_id']=$sid;
            $param['create_time']=date("Y-m-d H:i:s",$param['create_time']);
            $this->apiSuccess('回复成功',  $param,200);
        }else{
            $this->apiError('回复异常','',1001);
        }
    }
    /**
     * 找专家
     */
    public function search_expert()
    {
        $param = get_params();
        $where = array();
        if (!empty($param['name'])) {
            $where[] = ['a.name', 'like', '%' . $param['name'] . '%'];
        }
        if (!empty($param['industry_id'])) {
            $where[] = ['a.industry_id', '=', $param['industry_id']];
        }else{
            $where[] = ['a.industry_id', '>', 0];
        }
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $expert = Db::name('Admin')
            ->field('a.id,a.name,a.mobile,a.landline,a.thumb,a.zhicheng,a.desc,a.industry_id,a.praise')
            ->alias('a')
            ->where($where)
            ->order('a.id asc')
            ->paginate($rows, false, ['query' => $param])->toArray();
        //$expert->toArray();
        /*pr($expert);*/
        foreach($expert['data'] as $key=>$val){

            $expert['data'][$key]['server_num'] = Db::name('Recover')->where(['expert_id'=>$val['id']])->count();
            $expert['data'][$key]['praise'] = $expert['data'][$key]['praise']? $expert['data'][$key]['praise']:0;
        }
        //pr($expert);
        $this->apiSuccess('请求成功',  $expert,200);
        
    }
    /**
     * 找专家详情
     */
    public function expert_detail()
    {
        $param = get_params();
        $where = array();
        if (empty($param['id'])) {
            $this->apiError('专家编号不能为空','',1001);
        }
        $expert = Db::name('Admin')
            ->field('id,username,name,nickname,mobile,landline,thumb,desc,zhicheng,industry_id,intro')
            ->where(['id'=>$param['id']])
            ->find();
        //pr($expert);
        $this->apiSuccess('请求成功',  $expert,200);
        
    }
    /**
     * 用户留言
     */
    public function diagnosis_add()
    {
        $param = get_params();
        if (empty($param['industry_id'])) {
            //$this->apiError('领域编号不能为空','',1001);
            $param['industry_id']=0;
        }
        if (empty($param['title'])) {
            $this->apiError('内容不能为空','',1001);
        }else{
            $param['title'] = $this->get_keywords($param['title']);
        }
        /*if (empty($param['expert_id'])) {
            $this->apiError('回复的专家编号不能为空','',1001);
        }*/
        if (empty($param['file_ids'])) {
            
        }
        $param['status'] = 1;
        
        $param['create_time'] = time();
        $param['admin_id'] = $this->uid;
        //$sid = DiagnosisList::strict(false)->field(true)->insertGetId($param);
        $did = Db::name('Diagnosis')->strict(false)->field(true)->insert($param);
        if ($did) {
            
            $this->apiSuccess('留言成功',  $param,200);
        }else{
            $this->apiError('留言成功','',1001);
        }
    }
    /**
     * 关键词过滤
     * */
    public function get_keywords($str){
        header('Content-Type:text/html;charset=utf-8'); 
        $res = Db::name('keywords')->field('title')->select()->toArray();
        $titles = array_map('preg_quote', array_column($res, 'title'));
        $pattern = "/" . implode("|", $titles) . "/i";
        $replacement = '**';
        $str = preg_replace($pattern,$replacement,$str);
        return $str;exit;
        
    }
    /**
     * 领域列表
     */
    public function industry_list(){
        $param = get_params();
        $where = array();
        $industry = Db::name('Industry')
                ->field('id,title')
                ->select();
        $this->apiSuccess('请求成功',  $industry,200);
    }
    /**
     * 供需列表
     */
    public function supply_list(){
        $param = get_params();
        $where = array();
        if (!empty($param['keywords'])) {
            $where[] = ['a.title|a.cooperation|a.cooperation_type', 'like', '%' . $param['keywords'] . '%'];
        }
        if(!empty($param['cate_id'])){
            $where[] = ['a.cate_id','=',$param['cate_id']];
        }
        if (!empty($param['industry_id'])) {
            $where[] = ['a.industry_id', 'in', $param['industry_id']];
        }
        if (!empty($param['status'])) {
            $where[] = ['a.status', '=', $param['status']];
            //$where[] = ['a.status', '=', 2];
        }
        if (!empty($param['expert_id'])) {
            $where[] = ['a.expert_id', '=', $param['expert_id']];
        }
        /*if($this->uid!=1){
            //$where[] = ['id', '=', $this->uid];
            $expert = Db::name('admin')->where(['id' => $this->uid])->find();
            $where[] = ['a.industry_id', 'in', $expert['industry_id']];
        }*/
        $where[]=['a.cate_id', '=', 2];
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $note = NoteList::where($where)
            ->field('a.*,d.name,d.thumb')
            ->alias('a')
            //->leftjoin('industry c', 'a.industry_id = c.id')
            ->leftjoin('admin d', 'd.id = a.admin_id')
            //->leftjoin('file f', 'f.id = a.file_ids')
            //->order('a.end_time desc,a.sort desc,a.create_time desc')
            ->order('a.id asc')
            ->paginate($rows, false, ['query' => $param])
            ->each(function ($item, $key) {
                //$item->start_time = empty($item->start_time) ? '-' : date('Y-m-d', $item->start_time);
                //var_dump($item->create_time);exit;
                //$item->create_time = empty($item->create_time) ? '-' : date('Y-m-d H:i', int()$item->create_time);
                //pr($item->create_time);
                //$item->create_time = empty($item->create_time) ? '-' : date('Y-m-d',$item->create_time);
                //$item->end_time = empty($item->end_time) ? '-' : date('Y-m-d', int($item->end_time));
                /*$item->recover = Db::name('Recover_supply')
                                ->alias('r')
                                ->field('r.*,d.name,d.thumb,FROM_UNIXTIME(r.create_time,"%Y-%m-%d %h:%i") as create_time')
                                ->where(['r.diagnosis_id'=>$item->id])
                                ->leftjoin('admin d', 'd.id = r.add_admin')
                                ->select();*/
                $file_ids=explode(',', $item->file_ids);        
                $item->imgs = Db::name('file')
                                ->field('filepath')
                                ->where('id','in',$item->file_ids)
                                ->select()->toArray();
                $item->content = $this->mergeImageUrl($item->content);
            });
        $this->apiSuccess('请求成功',  $note,200);
    }
    /**
     * 供需详情
     */
    public function supply_detail(){
        $param = get_params();
        $where = array();
        
        if(!empty($param['id'])){
            $where[] = ['a.id','=',$param['id']];
        }else{
            $this->apiError('编号不能为空','',1001);
        }
        
        /*if($this->uid!=1){
            //$where[] = ['id', '=', $this->uid];
            $expert = Db::name('admin')->where(['id' => $this->uid])->find();
            $where[] = ['a.industry_id', 'in', $expert['industry_id']];
        }*/
        $where[]=['a.cate_id', '=', 2];
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $note = NoteList::where($where)
            ->field('a.*,d.name,d.thumb')
            ->alias('a')
            //->leftjoin('industry c', 'a.industry_id = c.id')
            ->leftjoin('admin d', 'd.id = a.admin_id')
            ->find()->toArray();
        if(empty($note['src'])){
            $note['src'] = '/storage/avatars/company.jpg';
        }
        $note['content'] = $this->mergeImageUrl($note['content']);
        //pr($note);
        $id = $param['id'];
        $recover=Db::name('Recover_supply')
                    ->alias('r')
                    ->field('r.*,d.name,d.thumb,FROM_UNIXTIME(r.create_time,"%Y-%m-%d %h:%i") as create_time')
                    ->where(['r.diagnosis_id'=>$id])
                    ->leftjoin('admin d', 'd.id = r.add_admin')
                    ->select();
        $note['recover']=$recover;
        $this->apiSuccess('请求成功',  $note,200);
    }

    /**
     * @param $content 富文本内容
     * @param $path  图片路径
     * @return string|string[]|null
     */
    public function mergeImageUrl($content)
    {
        $url = "https://nongke.xintongyun.ltd";//拼接图片路径
        $pregRule = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.jpg|\.jpeg|\.png|\.gif|\.bmp]))[\'|\"].*?[\/]?>/";

        $content = preg_replace($pregRule, '<img src="' . $url .'${1}" style="max-width:100%">', $content);
        //pr($content);
        return $content;
    }
    /**
     * 供需回复
     */
    public function supply_recover()
    {
        $param = get_params();
        /*if (empty($param['diagnosis_id'])) {
            $this->apiError('问诊编号不能为空','',1001);
        }*/
        if (empty($param['content'])) {
            $this->apiError('回复内容不能为空','',1001);
        }else{
            $param['content'] = $this->get_keywords($param['content']);
        }
        /*if (empty($param['expert_id'])) {
            $this->apiError('回复的专家编号不能为空','',1001);
        }*/
        if (empty($param['status'])) {
            $update['status'] = 2;
        }else{
            $update['status'] = $param['status'];
        }
        //$diagnosisRes = NoteList::where(['id'=>$param['diagnosis_id']])->find();
        //$param['industry_id'] = $diagnosisRes['industry_id'];
        $param['add_admin'] = $this->uid;
        $param['create_time'] = time();
        //$sid = DiagnosisList::strict(false)->field(true)->insertGetId($param);
        $sid = Db::name('Recover_supply')->strict(false)->field(true)->insert($param);
        if ($sid) {
            if (isset($param['diagnosis_id'])) {
                $where['id'] = $param['diagnosis_id'];
                Db::name('note')->strict(false)->field(true)->where($where)->update($update);
            }
            
            $param['recover_id']=$sid;
            $param['create_time']=date("Y-m-d H:i:s",$param['create_time']);
            $this->apiSuccess('回复成功',  $param,200);
        }else{
            $this->apiError('回复异常','',1001);
        }
    }
    /**
     * 点赞
     */
    public function praise_add()
    {
        $param = get_params();
        if (empty($param['expert_id'])) {
            $this->apiError('专家编号不能为空','',1001);
        }
        /*if (empty($param['expert_id'])) {
            $this->apiError('回复的专家编号不能为空','',1001);
        }*/
        $adminRes = Db::name('Admin')->where(['id'=>$param['expert_id']])->find();
        if($adminRes['praise']>0){
            $update['praise']=$adminRes['praise']+1;
        }else{
            $update['praise']=1;
        }
        $updateRes = Db::name('Admin')->strict(false)->field(true)->where(['id'=>$param['expert_id']])->update($update);
        if($updateRes){
            $this->apiSuccess('点赞成功',  $param,200);
        }else{
            $this->apiSuccess('点赞异常',  $param,1);
        }
        
    }
    /**
     * 企业诉求列表
     */
    public function question_list()
    {
        $param = get_params();
        $where = array();
        if (!empty($param['keywords'])) {
            $where[] = ['r.content', 'like', '%' . $param['keywords'] . '%'];
        }
        $where[] = ['r.status', '=', 2];
        $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
        $recover=Db::name('Recover_supply')
            ->alias('r')
            ->field('r.*,d.name,d.thumb,FROM_UNIXTIME(r.create_time,"%Y-%m-%d %h:%i") as create_time')
            ->where($where)
            ->leftjoin('admin d', 'd.id = r.add_admin')
            ->order('r.id desc')
            //->select()->toArray();
            ->paginate($rows, false, ['query' => $param])
            ->toArray();
            /*->each(function ($item, $key) {
                //pr($item);
                $recover['data'][$key]['recovers']=Db::name('Recover_company')
                ->alias('r')
                ->field('r.*,d.name,d.thumb,FROM_UNIXTIME(r.create_time,"%Y-%m-%d %h:%i") as create_time')
                ->where(['diagnosis_id'=>$item['id']])
                ->leftjoin('admin d', 'd.id = r.add_admin')
                ->order('r.id asc')
                ->select()->toArray();
                //pr($recover);
            });*/

        foreach($recover['data'] as $key=>$val){
            $recover['data'][$key]['recovers']=Db::name('Recover_company')
                ->alias('r')
                ->field('r.*,d.name,d.thumb,FROM_UNIXTIME(r.create_time,"%Y-%m-%d %h:%i") as create_time')
                ->where(['diagnosis_id'=>$val['id']])
                ->leftjoin('admin d', 'd.id = r.add_admin')
                ->order('r.id asc')
                ->select()->toArray();
        }
        //pr($recover);
        $this->apiSuccess('请求成功',  $recover,200);
        
    }
    /**
     * 企业诉求回复
     */
    public function recover_company()
    {
        $param = get_params();
        if (empty($param['diagnosis_id'])) {
            $this->apiError('问题编号不能为空','',1001);
        }
        if (empty($param['content'])) {
            $this->apiError('回复内容不能为空','',1001);
        }
        /*if (empty($param['expert_id'])) {
            $this->apiError('回复的专家编号不能为空','',1001);
        }*/
        if (empty($param['status'])) {
            $update['status'] = 2;
        }else{
            $update['status'] = $param['status'];
        }
        //$diagnosisRes = NoteList::where(['id'=>$param['diagnosis_id']])->find();
        //$param['industry_id'] = $diagnosisRes['industry_id'];
        $param['add_admin'] = $this->uid;
        $param['create_time'] = time();
        //$sid = DiagnosisList::strict(false)->field(true)->insertGetId($param);
        $sid = Db::name('Recover_company')->strict(false)->field(true)->insert($param);
        if ($sid) {
            /*if (isset($param['diagnosis_id'])) {
                $where['id'] = $param['diagnosis_id'];
                Db::name('note')->strict(false)->field(true)->where($where)->update($update);
            }*/
            
            $param['recover_id']=$sid;
            $param['create_time']=date("Y-m-d H:i:s",$param['create_time']);
            $this->apiSuccess('回复成功',  $param,200);
        }else{
            $this->apiError('回复异常','',1001);
        }
    }
    /**
     * 生成专家小程序码
     */
    public function create_code(){
        /*$path = get_config('filesystem.disks.public.url');
        pr($path);
        exit;*/
        $param = get_params();
        if (empty($param['page'])) {
            $this->apiError('页面地址不能为空','',1001);
        }
        if (empty($param['id'])) {
            $this->apiError('专家编号不能为空','',1001);
        }
        if(isset($param['scene'])){
            $scene=$param['scene'];
        }else{
            $scene="expert_id=".$param['id'];
        }
        if(isset($param['env_version'])){
            $user['env_version']=$param['env_version'];
        }else{
            $user['env_version']="release";
        }

        $expert = Db::name('Admin')
            ->field('id,username,name,nickname,mobile,landline,thumb,desc,zhicheng,industry_id,intro')
            ->where(['id'=>$param['id']])
            ->find();
        if(!empty($expert['intro'])){
            $this->apiSuccess('成功',  $expert,200);
            exit;
        }else{
            //pr($expert);
            $user['username']=$expert['username'];
            $user['id']=$expert['id'];
            $user['scene']=$scene;
        
            /*$page='pages/zhuanjia/zhuanjia'; .
            $card="10";*/
            $class = new \app\api\controller\Spreadcode();
            $mpcodeimg=$class->mpcode($param['page'],$param['id'],$user);
            
            $where['id']=$param['id'];
            $update['intro']="/".$mpcodeimg;
            Db::name('Admin')->strict(false)->field(true)->where($where)->update($update);
            $data['intro']=$update['intro'];
            $this->apiSuccess('生成成功',  $data,200);
        }
        
        //$this->assign('retdata',$dataret);
    }
    
}
